Modal = {
	$:[],
	$container:[],
	forms:{}, // store entered values for any form here.
	isOpen:function(){
		var result = false;
		if($('#pp_full_res').length > 0){
			result = true;
		}
		return result;
	},
	open:function(id){
		var wait = 0;
		if(Modal.isOpen()){
			Modal.close();
			wait = 350;
		}
		
		var focus = Modal.setting[id].focus;
		var height = Modal.setting[id].height;
		var width = Modal.setting[id].width;
		
		Modal.settings.changepicturecallback = function(){
			Modal.$ = $('#pp_full_res'); // set the $'d modal here
			Modal.$container = $('.pp_default'); // set the $'d modal container here
			
			Modal.restoreContents(); // look for saved content and restore it
			$('.pp_overlay').click(function(){Modal.close();}); // close the modal when click outside of it.
			$('.pp_content').css('height','0px');
			// calculate height numbers
			var ymod = (height - 34) / 2; 
			var top = +$('.pp_default').css('top').split('px')[0];
			
			// calculate width numbers
			var xmod = (width-174) / 2;
			var left = +$('.pp_default').css('left').split('px')[0];
			
			$('.pp_default').css('top',top-ymod+'px'); // set top
			$('.pp_default').css('left',left-xmod+'px'); // set left
			$('.pp_content').css('width',width + 'px'); // set width

			if(focus != ""){
				$("div.pp_content").find('[name='+focus+']').focus(); // focus
			}
			
			var key = $('#pp_full_res').find('div.filterSet-title').attr("id");
			key = key.substring(7,key.length);
			if(key in Modal.callback){
				Modal.callback[key]();
			}
			
			Modal.reposition(0);
			
			$(document).trigger('document:changed');
		};
		
		setTimeout(function(){
			$.prettyPhoto.open('#'+id,Modal.settings);
			// trigger document changed
			$(document).trigger('document:changed');
		},wait);
	},
	close:function(){
		Modal.saveContents();
		$.prettyPhoto.close();
	},
	saveContents:function(){
		var formID = $("#pp_full_res").find('.filterSet-title').attr('id');
		var formName = formID.substring(7,formID.length);
		Modal.forms[formName] = Fundr.getAjaxData($('div#pp_full_res'));
	},
	restoreContents:function(){
		var $modal = $('#pp_full_res');
		var formID = $modal.find('.filterSet-title').attr('id');
		var formName = formID.substring(7,formID.length);
		if(formName in Modal.forms){
			for(key in Modal.forms[formName]){
				var $input = $modal.find('[name='+key+']');
				if($input.length > 0 && $input.attr('type') != 'file'){
					if($input.attr('type') == "radio"){
						Radio.set(key,Modal.forms[formName][key]);
					} else {
						$input.val(Modal.forms[formName][key]);
					}
				}
			}
		}
	},
	setting:{},
	settings:{
		social_tools:false,
		opacity:.5,
		default_width:175,
		keyboard_shortcuts:false,
		default_height:400,
		allow_resize:false,
		show_title:false,
		modal:true // modal has to be programmtically closed;
	},
	init:function(id,focus,height,width){
		Modal.setting[id] = {};
		Modal.setting[id].focus = focus;
		Modal.setting[id].height = height;
		Modal.setting[id].width = width;
	},
	callback:{},
	getPosition:function(){
		Modal.windowHeight = $(window).height();
		Modal.windowWidth = $(window).width();
		Modal.modalHeight = Modal.$.children().children().height();
		Modal.modalWidth = Modal.$.children().children().width();
	},
	windowHeight:0,
	windowWidth:0,
	modalHeight:0,
	modalWidth:0,
	reposition:function(time){
		if(Modal.isOpen()){
			if(time != 0){
				time = time || 200;
			}
			Modal.getPosition();

			var left = Math.round((Modal.windowWidth - Modal.modalWidth)/2);
			var top = Math.round((Modal.windowHeight - Modal.modalHeight)/2) + $(window).scrollTop();
			if(time == 0){
				Modal.$container.css('left',left+'px');
				Modal.$container.css('top',top+'px');
			} else {
				Modal.$container.animate({'left':left+'px','top':top+'px'},time);
			}
		}
	}
};